package swu.cx.master_of_mental_arithmetic

import android.app.Application
import android.content.Context
import androidx.annotation.NonNull
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel

class MyViewModel(application: Application, handle: SavedStateHandle):AndroidViewModel(application) {
    private var handle:SavedStateHandle?=null
    private val Key_High_Score = "key_high_score"
    private val Key_Left_Number = "key_left_number"
    private val Key_Right_Number = "key_right_number"
    private val Key_Operatior = "key_operator"
    private val Key_Answer = "key_answer"
    private val Save_Shp_Data_Name = "save_shp_data_name"
    private val Key_Current_Score = "key_current_score"
    var win_flag = false
    init {
        if (!handle.contains(Key_High_Score)){
            val shp = getApplication<Application>().getSharedPreferences(Save_Shp_Data_Name,Context.MODE_PRIVATE)
            handle.set(Key_High_Score,shp.getInt(Key_High_Score,0))
            handle.set(Key_Left_Number,0)
            handle.set(Key_Right_Number,0)
            handle.set(Key_Operatior,"+")
            handle.set(Key_Answer,0)
            handle.set(Key_Current_Score,0)
        }
        this.handle = handle
    }
    public fun getLeftNumber()=handle?.getLiveData<Int>(Key_Left_Number)
    public fun getRightNumber()=handle?.getLiveData<Int>(Key_Right_Number)
    public fun  getOperator()=handle?.getLiveData<String>(Key_Operatior)
    public fun  getHighScore()=handle?.getLiveData<Int>(Key_High_Score)
    public fun  getCurrentScore()=handle?.getLiveData<Int>(Key_Current_Score)
    public fun getAnswer() = handle?.getLiveData<Int>(Key_Answer)
    fun generator(){
        val Level = 20
        val x = (1..Level).random()
        val y = (1..Level).random()
        if (x%2==0){
            getOperator()?.value = "+"
            if (x>y){
                getAnswer()?.value = x
                getLeftNumber()?.value = y
                getRightNumber()?.value = (x-y)
            }else{
                getAnswer()?.value = y
                getLeftNumber()?.value = x
                getRightNumber()?.value = (y-x)
            }
        }else{
            getOperator()?.value = "-"
            if (x>y){
                getAnswer()?.value = (x-y)
                getLeftNumber()?.value = x
                getRightNumber()?.value = y
            }else{
                getAnswer()?.value = (y-x)
                getLeftNumber()?.value = y
                getRightNumber()?.value = x
            }
        }
    }
    fun save(){
        val shp = getApplication<Application>().getSharedPreferences(Save_Shp_Data_Name,Context.MODE_PRIVATE)
        shp.edit().apply{
            getHighScore()?.value?.let { putInt(Key_High_Score, it) }
            apply()
        }
    }
    fun answerCorrect(){
        getCurrentScore()?.value = getCurrentScore()?.value?.plus(1)
        if(getCurrentScore()?.value!! > getHighScore()?.value!!){
            getHighScore()?.value = getCurrentScore()?.value
            win_flag = true
        }
        generator()
    }
}